extends Panel
var p1 :SpriteFrames = load("res://tscn/player1.tres")
var p2 :SpriteFrames = load("res://tscn/players.tres")
var none :SpriteFrames = load("res://tscn/none.tres")
@onready var sp :AnimatedSprite2D = $"1/create"
@onready var _acc :LineEdit = $"../login/acc"
signal _start_game
var on_no :String = "p2" #创建人物的框初始显示的模型
var on_selec :String = ""

func _ready():
	sp.sprite_frames = p2
	sp.play("atc6")

func l_and_r():
	if sp.sprite_frames == p1:
		sp.sprite_frames = p2
		sp.call_deferred("play","atc6")
		on_no = "p2"
	else :
		sp.sprite_frames = p1
		sp.call_deferred("play","atc6")
		on_no = "p1"

func display_player(pos :String, nam :String, toy :String): #这是客户端主体调用的显示处理函数
	match pos:                                             #收到服务器指令和数据后，在哪个位置显示哪个角色
		"1":
			if toy == "p1":
				$"2/2".sprite_frames = p1
				$"2/2".play("idle")
			elif toy == "p2":
				$"2/2".sprite_frames = p2
				$"2/2".play("idle")
			else :
				$"2/2".sprite_frames = none #————————>>>>没有就置空动画
			$"2/Label".text = nam
		"2":
			if toy == "p1":
				$"3/3".sprite_frames = p1
				$"3/3".play("idle")
			elif toy == "p2" :
				$"3/3".sprite_frames = p2
				$"3/3".play("idle")
			else :
				$"3/3".sprite_frames = none  #————————>>>>没有就置空动画
			$"3/Label".text = nam
		"0":                     #————————>>>>如果是空账号就显示为空
			$"2/2".sprite_frames = none
			$"2/Label".text = ""
			$"3/3".sprite_frames = none
			$"3/Label".text = ""

func p_pos() ->String:    #新建人物位置安放逻辑
	if $"2/Label".text == "":
		return "1"
	else:
		if $"3/Label".text == "":
			return "2"
		else :
			return "0"
	
		
		

func _on_create_pressed():  #新建人物的数据发送，在这里采集完数据后，发送给服务器
	if $"1/nam".text == "":
		$"../messbox".messbox("角色名不能为空！")
	else:
		var acc = _acc.text     #这里是账号，提醒服务器要加载哪个存档
		var pss = p_pos()      #这里是用本来密码的位置来代替位置信息，第一个空还是第二个空
		var nam :String = $"1/nam".text  #这里是名字
		var toy :String = on_no          #这里是模型
		$"..".tcp_link.put_var("c,%s,%s,%s,%s" %[acc,pss,nam,toy] ) #向服务器发送创建命令和数据----->

#—————————————————————————————————————————左右按钮—————————————————————————————————————————————————
func _on_r_pressed():
	l_and_r()

	



func _on_l_pressed():
	l_and_r()

#————————————————————————————————————————————————————————————————————————————————————————————————

func _on_start_pressed(): #开始游戏按钮发射开始游戏信号到game脚本
	if on_selec == "":
		$"../messbox".messbox("请选择或创建人物")
	elif on_selec == "1" and $"2/Label".text != "":
		_start_game.emit()
		on_selec == ""  #————>>>>>开始后置空选择，防止后续账号出bug
	elif on_selec == "2" and $"3/Label".text != "":
		_start_game.emit()
		on_selec == ""  #————>>>>>开始后置空选择，防止后续账号出bug
		
		
		   
	


func _on_cancel_pressed():
	visible = false
	

#———————————————————————————————————————选择人物——————————————————————————————————————————————————
func _on__01pos_pressed():
	on_selec = "1"
	if $"2/Label".text == "":
		return
	else:
		var acc = _acc.text 
		$"..".tcp_link.put_var("z,%s,1" %acc)



func _on__02pos_pressed():
	on_selec = "2"
	if $"3/Label".text == "":
		return
	else:
		var acc = _acc.text 
		$"..".tcp_link.put_var("z,%s,2" %acc)


#————————————————————————————————————————删除人物——————————————————————————————————————————————————————
func _on_del_2_pressed():
	var acc = _acc.text 
	$"..".tcp_link.put_var("x,%s,1" %acc)



func _on_del_3_pressed():
	var acc = _acc.text 
	$"..".tcp_link.put_var("x,%s,2" %acc)
